//logs.js
/**
 * create by Miko
 * 2018年8月3日
 * 打开界面首先判断是否是审核模式，是的话给予一个user,直接跳转
 * 不是审核模式在判断是否是自动登录模式，是的话直接跳转，不是的话显示登录界面
 * 数据库中需要记录：
 * 1.username
 * 2.id
 * 3.name
 * */
const {$Toast} = require('../../dist/base/index');
const app = getApp()
const httpsUrl = app.httpsPrefix;

Page({
    data: {
        usr: "", pwd: "", actions: [
            {
                name: '取消'
            }, {
                name: '现在去绑定',
                color: '#2d8cf0',
            }
        ],showLogin:false
    },

    onShow() {
        let user = wx.getStorageSync("username");
        wx.hideToast()

        if (user) {
            wx.switchTab({
                url: '../main/main'
            });
        }else{
            this.setData({showLogin:true})
        }
    },
    onLoad(options){
        console.log(options)
        if (options.scene){
            let user = wx.getStorageSync("username");
            if (!user) {
                return
            }
            wx.setStorageSync("order", options.scene);
            //跳转到未签名预览中
            wx.navigateTo({
                url: '../nosignpreview/nosignpreview'
            })
            return
        }
        wx.showToast({
            title: '获取数据中...',
            icon: 'loading',
            duration: 100000
        })
    },
    onPwdChange(event) {
        this.setData({buttonType: "primary", buttonName: "登录", pwd: event.detail.value})
    },
    onUserChange(event) {
        this.setData({buttonType: "primary", buttonName: "登录", usr: event.detail.value})
    },
    click_login() {
        if (this.data.usr.replace(/ /g, '') === '' || this.data.pwd.replace(/ /g, '') === '') {
            $Toast({
                content: "帐号或密码不得为空",
                type: 'error'
            });
            return
        }

        $Toast({
            content: "正在登录请稍后～",
            type: 'loading'
        });
        wx.request({
            url: httpsUrl,
            data: {query: 'vue_登录', 帐号: this.data.usr, 密码: this.data.pwd},
            success: ({data}) => {
                console.log(data)
                if (data.flag === '0') {
                    $Toast.hide()
                    $Toast({
                        content: data.message,
                        type: 'error'
                    });
                } else {
                    $Toast.hide();
                    $Toast({
                        content: '登录成功',
                        type: 'success'
                    });
                    wx.setStorageSync("username", data.data[0].username)
                    wx.setStorageSync("姓名", data.data[0]['姓名'])
                    wx.switchTab({
                        url: '../main/main'
                    })
                }
            }, fail(ee) {
                console.log(ee)
                let e = "网络错误,请检查网络"
                $Toast({
                    content: e,
                    type: 'error'
                });
            }
        })
    }
})
